﻿using ServerBase.Config;
using SvCore;
using SvGame.Objects;

namespace SvGame.Logic;

public static partial class GameLogic
{
    public static void Hp(this IRole me)
    {
        if (me is Player)
        {
            (me as Player)?.NtfHp();
        }
    }
    // 计算状态
    public static void HpSet(this Player me)
    {
        // todo HpSet 装备附加

        // 内力法力
        me.UsageGet(EUsageType.基本内功, out _, out var levelforce);
        me.UsageGet(EUsageType.基本法术, out _, out var levelspell);
        me.StatusMax.Force = 10 * levelforce;
        me.StatusMax.Mana = 10 * levelspell;

        // 气血精神
        var keeBase = me.Talent.Con + me.Talent.Str;
        var senBase = me.Talent.Spi + me.Talent.Int;

        var age = me.Age;
        var kee = true switch
        {
            _ when age < 14 => 10 * keeBase,
            _ when age < 30 => (age - 4) * keeBase,
            _ => 26 * keeBase,
        };
        kee += me.StatusMax.Force / 4;
        me.StatusMax.Kee = kee;

        var sen = true switch
        {
            _ when age < 14 => 10 * senBase,
            _ when age < 30 => (age - 4) * senBase,
            _ => 26 * senBase,
        };
        sen += me.StatusMax.Mana / 4;
        me.StatusMax.Sen = sen;

        // 食物饮水
        me.StatusMax.Food = keeBase * 10;
        me.StatusMax.Water = senBase * 10;

        // 使合法
        me.HpLegalize();
    }
    // hp合法
    public static void HpLegalize(this Player me)
    {
        // 最大值
        me.StatusMax.Kee = Math.Max(me.StatusMax.Kee, 100);
        me.StatusMax.Sen = Math.Max(me.StatusMax.Sen, 100);
        me.StatusMax.Force = Math.Max(me.StatusMax.Force, 1);
        me.StatusMax.Mana = Math.Max(me.StatusMax.Mana, 1);
        me.StatusMax.Food = Math.Max(me.StatusMax.Food, 100);
        me.StatusMax.Water = Math.Max(me.StatusMax.Water, 100);

        // 有效值
        me.StatusEff.Kee = Math.Min(me.StatusEff.Kee, me.StatusMax.Kee);
        me.StatusEff.Sen = Math.Min(me.StatusEff.Sen, me.StatusMax.Sen);

        // 当前值
        me.StatusCur.Kee = Math.Min(me.StatusCur.Kee, me.StatusEff.Kee);
        me.StatusCur.Sen = Math.Min(me.StatusCur.Sen, me.StatusEff.Sen);
        me.StatusCur.Force = Math.Min(me.StatusCur.Force, me.StatusMax.Force);
        me.StatusCur.Mana = Math.Min(me.StatusCur.Mana, me.StatusMax.Mana);
        me.StatusCur.Food = Math.Min(me.StatusCur.Food, me.StatusMax.Food);
        me.StatusCur.Water = Math.Min(me.StatusCur.Water, me.StatusMax.Water);
    }
    
    public static bool CanHeal(this Room room)
    {
        if (!room.Cfg.CanHeal)
        {
            return false;
        }
        if (GetCity(room.Cfg.CityUid, out var city))
        {
            if (!city.Cfg.CanHeal)
            {
                return false;
            }
        }
        return true;
    }
    // 心跳自动恢复
    public static int HpAutoHeal(this IRole me)
    {
        var flag = me._HpAutoHeal();
        if (flag > 0)
        {
            me.Hp();
        }
        return flag;
    }
    public static int _HpAutoHeal(this IRole me)
    {
        var flag = 0;
        // todo HpAutoHeal 条件判断 可能有房间不给恢复 debuff不给恢复
        if (me.Room.NoTime)
        {
            return -1;
        }
        if (!me.Room.CanHeal())
        {
            return flag;
        }
        if (me.IsFighting())
        {
            return flag;
        }

        var isPlayer = me is Player;
        // 食物饮水消耗
        if (isPlayer)
        {
            if (me.StatusCur.Food > 0)
            {
                me.StatusCur.Food -= 1;
                flag++;
            }
            if (me.StatusCur.Water > 0)
            {
                me.StatusCur.Water -= 1;
                flag++;
            }
        }

        // 玩家饮水不足
        if (isPlayer && me.StatusCur.Water <= 0)
        {
            return flag;
        }
        // 有效气血恢复 根骨/10
        me.StatusEff.Kee += (me.Talent.Con / 10);
        me.StatusEff.Kee = Math.Min(me.StatusEff.Kee, me.StatusMax.Kee);
        flag++;

        // 气血恢复 根骨+内力/10
        me.StatusCur.Kee += (me.Talent.Con + me.StatusCur.Force / 10);
        me.StatusCur.Kee = Math.Min(me.StatusCur.Kee, me.StatusEff.Kee);
        flag++;

        // 内力恢复 内功有效等级/2
        if (me.StatusCur.Force < me.StatusMax.Force)
        {
            me.UsageGet(EUsageType.基本内功, out _, out var levelforce);
            me.StatusCur.Force += levelforce/2;
            me.StatusCur.Force = Math.Min(me.StatusCur.Force, me.StatusMax.Force);
            flag++;
        }

        // 玩家食物不足
        if (isPlayer && me.StatusCur.Food <= 0)
        {
            return flag;
        }
        // 有效精神恢复 灵性/10
        me.StatusEff.Sen += (me.Talent.Spi / 10);
        me.StatusEff.Sen = Math.Min(me.StatusEff.Sen, me.StatusMax.Sen);
        flag++;

        // 精神恢复 灵性+法力/10
        me.StatusCur.Sen += (me.Talent.Spi + me.StatusCur.Mana / 10);
        me.StatusCur.Sen = Math.Min(me.StatusCur.Sen, me.StatusEff.Sen);
        flag++;

        // 法力恢复 法术有效等级/2
        if (me.StatusCur.Mana < me.StatusMax.Mana)
        {
            me.UsageGet(EUsageType.基本法术, out _, out var levelforce);
            me.StatusCur.Mana += levelforce / 2;
            me.StatusCur.Mana = Math.Min(me.StatusCur.Mana, me.StatusMax.Mana);
            flag++;
        }

        return flag;
    }
}
